import Vue from 'vue';
import Vue2Filters from 'vue2-filters';
var mpheader = require("../components/mpheader.js");
var myVerification = require("../components/verification.js");
var dt = require("../components/dateTime.js");
var wl = require("../components/winLocation.js");

var recordVue;
var openid, prdtNo, sceneId;
var min_cnt = 0;
var max_cnt = 50;
var recordData = [];
var winWidth = window.innerWidth;
var ismaxping = true; // 是否是大屏手机
// 身份证正则表达式
var idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
var djs = 60;
var webOrder_ids = ""; // 网页版查出的订单号


$(document).ready(function() {
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        // 通过下面这个API隐藏右上角按钮
        wx.hideOptionMenu();
    });
    if (winWidth < 350) {
        ismaxping = false;
    }
    initPage();
});
var orderStateMap = {
    "01": "申请中",
    "03": "申请成功，请等待资金入账",
    "04": "划付失败，信用卡额度已释放",
    "05": "划付失败，信用卡额度已释放",
    "06": "申请成功、借款资金已划付",
    "99": "申请失败"
};

// 处理数据
function manipulationData(lfqorderInfo) {
    webOrder_ids = "";
    for (var i = 0, l = lfqorderInfo.length; i < l; i++) {
        lfqorderInfo[i].formatDate = dt.getDateStrFormate1(lfqorderInfo[i].serial_no_time, "date");
        lfqorderInfo[i].stage_msg = orderStateMap[lfqorderInfo[i].order_state];

        if (parseInt(lfqorderInfo[i].order_state) == 6 || parseInt(lfqorderInfo[i].order_state) == 3) {
            lfqorderInfo[i].pngName = "../images/creditLoan/chenggong_icon.png";
            lfqorderInfo[i].msgClass = "chenggong weui-cell__ft";

        } else {
            lfqorderInfo[i].pngName = "../images/creditLoan/sb_icon.png";
            lfqorderInfo[i].msgClass = "shibai weui-cell__ft";
            lfqorderInfo[i].out_stage_count = "0";
        }
        if (recordVue.queryRecord == "query") {
            if (i != 0) {
                webOrder_ids += ",";
            }
            webOrder_ids += lfqorderInfo[i].order_id;
        }
    }

    recordVue.rateArray = lfqorderInfo;
}

// 设置记录父标签的高度
function setRecordParentHeight() {
    var wih = window.innerHeight;
    var rph = (wih - 44 - 46) + "px";
    $("#loanInquiry_page>.page__bd").css("height", rph);
}

// 微信查询条件 也是默认查询条件
var xwrdParamenter = {
    open_id: openid,
    prdt_no: prdtNo,
    min_count: min_cnt,
    max_count: max_cnt,
    servlet_type: "query_order",
    now: new Date().getTime()
};

// 取交易记录
function getRecordData(rdParameter) {

    $.post(LFQCreateOrderServlet, rdParameter, function(rddata) {
        recordVue.showDialog = 0;
        rddata = JSON.parse(rddata);
        if (rddata.lft_orderinfo_response && rddata.lft_orderinfo_response.lfqorderInfo_arrays.lfqorderInfo) {
            var thelfqorderInfo = rddata.lft_orderinfo_response.lfqorderInfo_arrays.lfqorderInfo;
            manipulationData(thelfqorderInfo);
            if (thelfqorderInfo.length && thelfqorderInfo.length > 0) {
                if (recordVue.queryRecord == "query") {
                    recordVue.queryRecord = "record";
                    recordVue.recordFrom = "web";
                    recordVue.btnmsg = "返回";
                }
            } else {
                recordVue.prompt = "未查到相关订单";
                recordVue.showDialog = 2;
                bingingOrderId("iconInfoCircle");
            }
        } else {
            if (rddata.error_response && rddata.error_response.resp_msg) {
                recordVue.prompt = rddata.error_response.resp_msg;
            } else {
                recordVue.prompt = "未查到相关订单";
                bingingOrderId("iconInfoCircle");
            }

            recordVue.showDialog = 2;
        }


    });
}
// 
function craeteRecordVue() {
    var mheder1 = mpheader.getMPheader({
        "titleText": "小通秒贷查询"
    }, function() {
        wx.closeWindow();
    });
    Vue.use(Vue2Filters);
    recordVue = new Vue({
        el: "#loanInquiry_page",
        data: {
            rateArray: recordData,
            pingsize: ismaxping,
            name: "",
            istsName: false,
            userId: "",
            istsSFZ: false,
            userIdFormat: "",
            phoneNo: "",
            istsPhoneNo: false,
            phoneFormat: "",
            vcode: "",
            istsVcode: false,
            dxbtnmsg: "获取验证码",
            btnmsg: "查询网页版秒贷记录",
            queryRecord: 'record', // record:显示记录 , query: 显示查询条件
            recordFrom: 'wx', // wx: 当前列表是微信查的,  web: 当前列表是网页查的
            showDialog: 1, // 0:不显示弹框 1:显示加载效果 2: 显示常用提示弹框 3: 显示是否绑定
            prompt: "",
            iconSuccess: false,
            iconCancel: false,
            iconInfoCircle: false

        },
        computed: {

        },
        beforeMount: function() {
            console.log("amountslect beforeMount 渲染前");
        },
        mounted: function() {
            console.log("amountslect mounted 加载完");

            getRecordData(xwrdParamenter);
        },
        components: {
            "mpheader": mheder1
        },
        methods: {
            "toDetails": function(event) {
                var theOpenid = openid;
                if(this.recordFrom == "web"){
                    theOpenid = "0201H5";
                }
                var serialNo = $(event.target).parents(".weui-cells").attr("data-serial");
                location.href = "./details.html?openid=" + theOpenid + "&serialNo=" + serialNo + "&sceneId=" + sceneId + "&prdtNo=" + prdtNo;
            },
            "switchInterface": function() {
                if (this.recordFrom == "wx" && this.queryRecord == "record") {
                    // 当前微信查询的记录转到查询条件
                    this.queryRecord = "query";
                } else if (this.recordFrom == "wx" && this.queryRecord == "query") {
                    // 当前查询条件转到微信查询的记录
                    setTimeout(function  () {
                        this.queryRecord = "record";
                    }.bind(this),300);
                    
                } else if (this.recordFrom == "web") {
                    // 当前网页查询记录
                    this.showDialog = 3;
                }
            },
            "nameInput": function() {

                var isn = iscnName();
                if (isn) {
                    this.istsName = false;
                }
            },
            "nameInputChange": function() {
                this.name = this.name.replace(/[^\u4e00-\u9fa5]/g, "");
                var isn = iscnName();
                if (isn) {
                    this.istsName = false;
                } else {
                    this.istsName = true;
                }

            },
            "userIdInput": function(event) {
                this.userId = this.userId.replace(/\*/g, "X").replace(/[^0-9|X|x]/g, "");;
                userIdFormat(event);
                if (idRegExp.test(this.userId)) {
                    this.istsSFZ = false;
                }
            },
            "userIdChange": function() {

                if (!idRegExp.test(this.userId.replace(/\s/g, ""))) {
                    this.istsSFZ = true;
                }
                if (this.userId.length < 14) {
                    this.userId = this.userId.replace(/(\d{6})/, "$1  ");
                } else {
                    this.userId = this.userId.replace(/(\d{6})(\d{8})/, "$1  $2  ");
                }

            },
            "phoneInput": function(event) {
                // 手机输入框输入事件
                if (this.phoneNo.search(/\D/) != -1) {
                    this.phoneNo = this.phoneNo.replace(/\D/g, "");
                }
                this.phoneFormat = userPhoneFormat(event);
                if (this.phoneNo.length == 11) {
                    if (djs == 60) {
                        $("#getVcode_btn").removeClass("disabled");
                    }
                    this.istsPhoneNo = false;
                } else {
                    $("#getVcode_btn").addClass("disabled");
                }
            },
            "phoneInputChange": function() {
                // 手机输入框改变事件
                if (this.phoneNo.length != 11) {
                    this.istsPhoneNo = true;
                } else {
                    this.istsPhoneNo = false;
                }
                if (this.istsPhoneNo.length > 3 && this.istsPhoneNo.length < 7) {
                    this.istsPhoneNo = this.istsPhoneNo.replace(/(\d{3})/, "$1  ");
                } else if (this.istsPhoneNo.length > 8) {
                    this.istsPhoneNo = this.istsPhoneNo.replace(/(\d{3})(\d{4})/, "$1  $2  ");
                }
            },
            "vcodeInput": function() {
                if (this.vcode.search(/\D/) != -1) {
                    this.vcode = this.vcode.replace(/\D/g, "");
                }
                if (this.vcode.length == 4) {
                    this.istsVcode = false;
                }
            },
            "vcodeInputChange": function() {
                if (this.vcode.length == 4) {
                    this.istsVcode = false;
                } else {
                    this.istsVcode = true;
                }
            },
            "inputfocus": function(event) {
                $(event.target).parent().parent().find("p.input_format").show();
            },
            "inputblur": function(event) {
                $(event.target).parent().parent().find("p.input_format").hide();
            },
            "getVcode": function() {
                // 取验证码
                
                var $getVcodeBtn = $("#getVcode_btn");

                if ($getVcodeBtn.hasClass("disabled")) {
                    return;
                } else {
                    $getVcodeBtn.addClass("disabled");
                    this.dxbtnmsg = "重新获取(" + djs + ")";
                    var gvcHande = setInterval(function() {
                        djs--;
                        this.dxbtnmsg = "重新获取(" + djs + ")";
                        if (djs == 0) {
                            this.dxbtnmsg = "重新获取";
                            $getVcodeBtn.removeClass("disabled");
                            clearInterval(gvcHande);
                            djs = 60;
                        }
                    }.bind(this), 1000);

                    var vcParments = {
                        hp_no: this.phoneNo,
                        openid: "0201H5",
                        prdt_no: "0201"
                    };

                    $.post(LFQ_SMS_SEND_URL, vcParments, function(vcdata) {
                        console.dir(vcdata);
                    });

                }

            },
            "queryWebRecord": function() {
                // 点击确定拼接检查网页记录查询条件发起查询
                
                if (!idRegExp.test(this.userId.replace(/\s/g, ""))) {
                    this.istsSFZ = true;
                }
                if (this.phoneNo.length != 11) {
                    this.istsPhoneNo = true;
                }
                if (this.vcode.length != 4) {
                    this.istsVcode = true;
                }
                if (idRegExp.test(this.userId.replace(/\s/g, "")) && this.phoneNo.length == 11 && this.vcode.length == 4) {
                    var webParament = {
                        servlet_type: "query_order_web",
                        open_id: "0201H5",
                        id_no: this.userId.replace(/\s/g, ""),
                        hp_no: this.phoneNo,
                        prdt_no: "0201",
                        verify_code: this.vcode,
                        min_count: 0,
                        max_count: 50
                    };
                    getRecordData(webParament);
                }
            },
            "hide": function() {
                //  隐藏提示框
                this.showDialog = 0;
                if (this.recordFrom == "web") {
                    wx.closeWindow();
                }
            },
            "binding": function() {
                // 把查出的订单绑定到当前微信
                this.showDialog = 1;
                var bindParament = {
                    servlet_type: "band_order",
                    open_id: openid,
                    order_ids: webOrder_ids
                };
                $.post(LFQCreateOrderServlet, bindParament, function(bindData) {
                    bindData = JSON.parse(bindData);
                    console.dir(bindData);
                    if (bindData.lft_orderinfo_response && bindData.lft_orderinfo_response.resp_code == "0000") {
                        recordVue.prompt = "绑定成功";
                        bingingOrderId("iconSuccess");
                    } else {
                        recordVue.prompt = "绑定失败,请稍后再试";
                        bingingOrderId("iconCancel");
                    }
                    recordVue.showDialog = 2;
                });
            }
        }

    });
}

//  设置提示内容中的小图片
function bingingOrderId(iconClass) {
    recordVue.iconSuccess = false;
    recordVue.iconCancel = false;
    recordVue.iconInfoCircle = false;
    if (iconClass == "iconSuccess") {
        recordVue.iconSuccess = true;
    } else if (iconClass == "iconCancel") {
        recordVue.iconCancel = true;
    } else if (iconClass == "iconInfoCircle") {
        recordVue.iconInfoCircle = true;
    }
}

// 身份证格式提示
function userIdFormat(event) {
    var thisVal = $(event.target).val();
    thisVal = thisVal.replace(/[^0-9|X|x]/g, "");
    thisVal = thisVal.replace("x", "X");

    var formatVal = thisVal;
    if (thisVal.length > 6 && thisVal.length < 14) {
        formatVal = thisVal.replace(/(\d{6})/, "$1  ");
    } else if (thisVal.length > 14) {
        formatVal = thisVal.replace(/(\d{6})(\d{8})/, "$1  $2  ");
    }

    recordVue.userIdFormat = formatVal;
    return formatVal;
}

// 手机格式化提示
function userPhoneFormat(event) {
    var thisVal = $(event.target).val();
    var phoneFormat = thisVal.replace(/\D/g, "");

    if (thisVal.length > 3 && thisVal.length < 7) {
        phoneFormat = thisVal.replace(/(\d{3})/, "$1  ");
    } else if (thisVal.length > 8) {
        phoneFormat = thisVal.replace(/(\d{3})(\d{4})/, "$1  $2  ");
    }

    return phoneFormat;
}


// 判断是否2-8个中文
function iscnName() {
    var pdjieguo = false;
    var iscnName = myVerification.isCnName(recordVue.name);
    if (iscnName && recordVue.name.length > 1 && recordVue.name.length < 9) {
        pdjieguo = true;
    }

    return pdjieguo;
}



// 初始化变量
function initValue() {
    openid = wl.QueryString.GetValue("openid");
    prdtNo = wl.QueryString.GetValue("prdtNo");
    sceneId = wl.QueryString.GetValue("sceneId");
    xwrdParamenter.open_id = openid;
    xwrdParamenter.prdt_no = prdtNo;
}

function initPage() {
    wl.checkBrowser();
    wl.QueryString.Initial();
    initValue();
    setRecordParentHeight();

    craeteRecordVue();

}